/*
Created by Borui Wang (bwang29@wisc.edu)
March, 2012, GoCuro
main.js
*/

var WS = new webUtil();
var items = {};
var target_item_id = 0;

$(document).ready(DOMREADY);

function DOMREADY(){
	session_c = persist_read();
	if(session_c.gocuro_username!== "" && session_c.gocuro_username!== "undefined" && session_c.gocuro_username!== "Invalid"){
		$('#login_username').val(session_c.gocuro_username);
		$('#login_password').val(session_c.gocuro_password);
		$('#login_wait').fadeIn();
		$('#newuser_create_success_msgbox').fadeOut();
		$('#newuser_login_failure_msgbox').fadeOut();
		var targetUrl = host+"login";
		var postInstance = [];
		postInstance.username = session_c.gocuro_username;
		postInstance.pwd = session_c.gocuro_password;
		WS.makeGenericPOSTRequest(targetUrl, WS.toParam(postInstance), userLoginPostHandle,"","");
	}else{
		$('#login_wait').fadeOut();
		$('#newuser_create_success_msgbox').html("Please login manually");
		$('#newuser_create_success_msgbox').fadeIn();
	}
}

function initNewUserView(){
	$('#newUser').modal();
	getCompaniesGet();
}


function editNewUserView(){
	$('#editUser').modal();
	getUserPost();
}


function initImportInventoryView(){
	session_c = persist_read();
	if(session_c.gocuro_username!== "" && session_c.gocuro_username!== "undefined" && session_c.gocuro_username!== "Invalid"){
		$('#csvimportiframe').html('<iframe id="export_iframe" src="csvupload.html" width="100%" height="140px" style="border:none;  overflow:hidden"></iframe>');
		// inject form action in the iframe, not that source must be within the same origin
		document.getElementById('export_iframe').onload = function () {
			$("#export_iframe").contents().find("#csv_export_form").attr("action", host +"csv/import?username=" + session_c.gocuro_username +"&pwd="+session_c.gocuro_password);
			 
			 //alert("http://gocuro.herokuapp.com/csv/import?username=" + session_c.gocuro_username +"&pwd="+session_c.gocuro_password);
		};
	$('#importInventory').modal();
	}else{
		alert("Please login again.");	
	}

}

function initExportInventoryView(){
	$('#exportInventory').modal();
}

function initEditInventoryView(id){
	
	target_item_id = id.substring(4);
	$('#edititem_name').val(items["item_"+target_item_id].name);
	$('#edititem_quantity').val(items["item_"+target_item_id].quantity);
	$('#edititem_barcode').val(items["item_"+target_item_id].barcode_id);
	$('#editInventory').modal();
}

function initDeleteInventoryView(id){
	target_item_id = id.substring(4);
	$('#delete_confirm_id').html(target_item_id);
	$('#deleteInventory').modal();
}


function persist_clear(){
	setCookie("gocuro_password","",365);
	setCookie("gocuro_username","",365);
	setCookie("gocuro_user_id","",365);
	setCookie("gocuro_jobsite_id","",365);
	setCookie("gocuro_jobsite_name","",365);
	setCookie("gocuro_device_id","",365);
}

function persist_write(gocuro_password,gocuro_username,gocuro_user_id,gocuro_jobsite_id,gocuro_jobsite_name,gocuro_device_id){
	setCookie("gocuro_password",gocuro_password,365);
	setCookie("gocuro_username",gocuro_username,365);
	setCookie("gocuro_user_id",gocuro_user_id,365);
	setCookie("gocuro_jobsite_id",gocuro_jobsite_id,365);
	setCookie("gocuro_jobsite_name",gocuro_jobsite_name,365);
	setCookie("gocuro_device_id",gocuro_device_id,365);
}


function persist_read(){
	var session_c = {};
	session_c.gocuro_password = getCookie("gocuro_password");
	session_c.gocuro_username = getCookie("gocuro_username");
	session_c.gocuro_user_id = getCookie("gocuro_user_id");	
	session_c.gocuro_jobsite_id = getCookie("gocuro_jobsite_id");	
	session_c.gocuro_jobsite_name = getCookie("gocuro_jobsite_name");
	session_c.gocuro_device_id = getCookie("gocuro_device_id");	
	return session_c;
}